<?php
namespace app\admin\controller;

use think\Controller;

class Login extends Controller
{
    public function index()
    {
        if (request()->isPost()) {
            if (!input('?param.username') || !input('?param.password'))
            {
                $this->error('用户名和密码不能为空');
            }
            $username = input('param.username');
            $password = input('param.password');
            $where['username'] = $username;
            $where['status'] = 1;
            $admin = model('Admin')->get($where);
            if ($admin) {
                // 检查密码
                if ($admin->password != md5($admin->rand_char . $password)) {
                    $this->error('密码错误');
                }

                // 检查状态
                if ($admin->status == 2) {
                    $this->error('用户被禁用，请联系管理员');
                }

                // 登录成功，记录session
                session('id', $admin['id'], 'admin');
                session('username', $admin['username'], 'admin');

                $this->success('登录成功！', url('index/index'));
            } else {
                $this->error('用户名不存在');
            }
        }
        return view();
    }

    /**
     * 退出登录
     */
    public function logout() {
        session(null, 'admin');
        $this->success('退出登录成功！', url('login/index'));
    }
}
